package controller;

import model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import service.MasterService;
import service.StudentService;
import service.UserService;
import util.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

@Controller
public class UserController {
    @Autowired

    private UserService userService;
    @Autowired

    private MasterService masterService;


    @RequestMapping("login")

    public String toLogin() {

        return "login";

    }


    @RequestMapping("check_login")
    public String check_login(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
//        String username = request.getParameter("username");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("username=" + username);

        System.out.println("password=" + password);
        User user = userService.check_login(username, password);
        if (user != null) {

            model.addAttribute("username", username);
            return "success";

        } else
            return "false";

    }


    @RequestMapping("play_list")

    public String getPage(@RequestParam(defaultValue = "1") int currentPage, Model model,

                          HttpServletRequest request, String username_visiter, String username_homer,String player,String username) {


        player=request.getParameter("username");

        int play_state=2;
        System.out.println("check="+player);



        game_list game_list=new game_list();
        play_list play_list=new play_list();
        play_list play_list_2=new play_list();
     //   game_list.setPlayer(username);
     //   game_list.setPlay_state(state);
       game_list= userService.find_list_by_name_state(player,play_state);
       System.out.println(game_list);

       if(game_list!=null){
           play_list.setGame_name(game_list.getGame_name());
           play_list.setGame_type(game_list.getGame_type());
           play_list.setUsername_homer(game_list.getPlayer());
           System.out.println("play_list:"+play_list);
           userService.insert_play_list(play_list);
           System.out.println("yes1");
           userService.delete(game_list.getId());
           System.out.println("yes2");
       }


        int home_state=1;
        home_list home_list = userService.find_home_list_by_visiter_state(player,home_state);
        System.out.println("添加的home_list为="+home_list);


        if(home_list!=null){
            play_list.setUsername_homer(home_list.getHomer());

            play_list.setUsername_visiter(home_list.getVisiter());

            play_list.setGame_type(home_list.getGame_type());
            play_list.setGame_name(home_list.getGame_name());
            System.out.println("home_play_list:"+play_list);

            userService.update_play_list(play_list);
           // userService.insert_play_list(play_list);
           // System.out.println("yes1");
         //   userService.delete(game_list.getId());
         //   System.out.println("yes2");
        }





        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("username_homer", username_homer);

        map.put("username_visiter", username_visiter);


        Page<play_list> page = userService.findByPage(currentPage, map);

        model.addAttribute("pageData", page);//设置一个属性

        model.addAttribute("params", map);
        model.addAttribute("username",username);
        return "play_list";

    }


    @RequestMapping("to_new_apply")

    public String toAdd( String username,Model model) {

        List<game_list> game_lists=null;
        game_lists = userService.get_game_lists();
        System.out.println(game_lists);

        model.addAttribute("game_lists",game_lists);

        model.addAttribute("username",username);
        return "new_apply";

    }



    @RequestMapping("new_apply")

    public String new_apply(HttpServletRequest request,game_list game_list,Model model) throws Exception {
        model.addAttribute("username",request.getParameter("username"));
       // String play_state=request.getParameter("game_state");
      //  game_list.setPlay_state(play_state);

        userService.new_apply(game_list);

          return "play_list";

    }


    @RequestMapping("check_home")
    public String Check_home(int id,Model model,HttpServletRequest request) throws Exception {
        System.out.println(id);
        String username=request.getParameter("username");
       play_list play_list=userService.find_play_list_ById(id);
        model.addAttribute("username",username);
        model.addAttribute("data",play_list);

        return "home";


    }

    @RequestMapping("join")
    public String join(int id,Model model,HttpServletRequest request){
        System.out.println("id="+id);
        play_list play_list=userService.find_play_list_ById(id);

        String username=request.getParameter("username");
        String homer=play_list.getUsername_homer();
        String visiter=play_list.getUsername_visiter();

        System.out.println("username="+username);
        System.out.println("homer="+homer);
        System.out.println("play_list="+play_list);


        home_list home_list=new home_list();
        home_list.setHomer(play_list.getUsername_homer());
        home_list.setVisiter(username);
        home_list.setGame_name(play_list.getGame_name());
        home_list.setGame_type(play_list.getGame_type());
        home_list.setHome_state(0);


        System.out.println(home_list);




        if(username.equals(homer)){
            System.out.println("你不能加入自己创建的陪玩间");
            model.addAttribute("hint","你不能加入自己创建的陪玩间");
            model.addAttribute("username",username);
            return "join_false";


        }
        else if(username.equals(visiter)){
            System.out.println("你已经加入该房间，请勿重复加入");
            model.addAttribute("hint","你已经加入该房间，请勿重复加入");
            return "join_false";

        }
        else if(play_list.getUsername_visiter()==null){

//            System.out.println("no1");
//            play_list.setUsername_visiter(username);
//            System.out.println("no2");
//            System.out.println("play_list==="+play_list);
//            System.out.println("no3");
//
//
//
//            userService.visiter_login(play_list);
//            System.out.println("no4");
//            System.out.println("Visite="+username);
//            model.addAttribute("username",username);


             userService.insert_home_list(home_list);
             model.addAttribute("home_list",home_list);
             return "play_list";





        }
        else{
            System.out.println("该房间已满");
            model.addAttribute("hint","该房间已满");
            return "join_false";
        }



    }




    @RequestMapping("home_menu")
    public String home_menu(Model model,String username){


        List<home_list>home_list=userService.find_game_list_ByName(username);

        System.out.println("home_lists="+home_list);

        model.addAttribute("username",username);
        model.addAttribute("home_list",home_list);
     return "home_menu";

    }





    @RequestMapping("home_menu_agree")
    public String home_menu_agree(int id,String username,Model model){

//     model.addAttribute("home_list",home_list);




        System.out.println("id="+id);
        System.out.println("username="+username);

        home_list home_list=userService.find_home_list_byId(id);
        System.out.println(home_list);


        information information =new information();
        information.setUsername(home_list.getVisiter());
        information.setInformation("User:"+username+
                " Agree you to join it for the type:  "+home_list.getGame_type()+" The name is  "+home_list.getGame_name()+"  Play room ");
        userService.insert_information(information);



        userService.change_home_list_state(home_list);

        System.out.println(home_list);

        return "home_menu";

    }


    @RequestMapping("home_menu_refuse")
    public String home_menu_refuse(int id,String username,Model model){

//     model.addAttribute("home_list",home_list);




        System.out.println("id="+id);
        System.out.println("username="+username);

        home_list home_list=userService.find_home_list_byId(id);
        System.out.println(home_list);

        information information =new information();
        information.setUsername(home_list.getVisiter());
        information.setInformation("User:"+username+
                " Refused you to join it for the type:  "+home_list.getGame_type()+" The name is  "+home_list.getGame_name()+"  Play room ");
        userService.insert_information(information);

        System.out.println(home_list);

        return "home_menu";

    }








    @RequestMapping("follow_homer")

    public String  follow_homer(String homer,String username,Model model)throws Exception {

        follower_list follower_list = new follower_list();
        System.out.println("username=" + username);
        System.out.println("homer=" + homer);
        follower_list.setUsername(username);
        follower_list.setFollower(homer);
        System.out.println("3");

        fan_list fan_list = new fan_list();
        fan_list.setUsername(homer);
        fan_list.setFan(username);

        if (homer.equals(username)) {
            model.addAttribute("tishi", "你不能关注自己");
            return "false";
        } else {
            System.out.println(follower_list);
            System.out.println(fan_list);
            userService.insert_follower_list(follower_list);
            System.out.println("1");
            userService.insert_fan_list(fan_list);
            System.out.println("2");

            return "home";

        }


    }

    @RequestMapping("to_follow_list")
    public String follow_list(String username,Model model){

        List<follower_list> follower_list=userService.find_follow_list_byName(username);
        model.addAttribute("follower_list",follower_list);
        model.addAttribute("username",username);
        return "follow";


    }

    @RequestMapping("not_follow")

    public String not_follow(String username,String follower,Model model){
        System.out.println("username="+username);
        System.out.println("follower="+follower);

        follower_list follower_list =new follower_list();
        fan_list fan_list=new fan_list();
        follower_list.setUsername(username);
        follower_list.setFollower(follower);
        fan_list.setUsername(follower);
        fan_list.setFan(username);
        userService.not_follow(follower_list);
        userService.not_fan(fan_list);





        model.addAttribute("username",username);
        return "follow";


    }









    @RequestMapping("to_fan_list")
    public String fan_list(String username,Model model){

        List<fan_list> fan_list=userService.find_fan_list_byName(username);
        model.addAttribute("fan_list",fan_list);

        return "fan";


    }



    @RequestMapping("to_information")
    public String to_information(String username,Model model){


        List<information> information=userService.find_information_byName(username);
        model.addAttribute("information",information);
        model.addAttribute("username",username);
       return "information";
    }

}